﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinLogicUniversity
{
    public partial class frmUpdateInventory : Form
    {
        private List<PurchaseOrder> pOrders;
        List<PurchaseDetail> purchaseDetails;

        LogicUniversityT2Entities ct = new LogicUniversityT2Entities();






      
        public frmUpdateInventory()
        {
            InitializeComponent();

            libtoday.Text= DateTime.Now.ToString("dd/MM/yyyy");

            var a = ct.PurchaseOrders;
            List<PurchaseOrder> purchaseOrders = a.ToList();
            pOrders = purchaseOrders;
            cboPOID.DataSource = purchaseOrders;
            cboPOID.DisplayMember = "PurchaseOrderID";

            //var a = ct.PurchaseDetails;
            //cboPOID.DataSource = a;
            //cboPOID.DisplayMember = "PurchaseOrderID";

            
        }

        private void selectPO(String poNumber)
        {
            PurchaseOrder selectedPO = pOrders.Find(po => po.PurchaseOrderID == poNumber);
            if (selectedPO != null)
            {
                purchaseDetails = selectedPO.PurchaseDetails.ToList();
                List<Item> items = new List<Item>();
                foreach (PurchaseDetail pd in purchaseDetails)
                {
                    items.Add(pd.Item);
                }

                cboItemDetail.DataSource = items;
                cboItemDetail.DisplayMember = "ItemDescription";
            }
        }
     
        private void cboPOID_SelectedIndexChanged(object sender, EventArgs e)
       {
        selectPO(cboPOID.Text);
        

   //  //  string pd = cboPOID.SelectedValue.ToString();
   //PurchaseDetail c = (PurchaseDetail)cboPOID.SelectedItem;

            
   //         var aa = from pod in ct.PurchaseDetails
   //                  where pod.PurchaseOrderID == c.PurchaseOrderID
   //                  select new

   //                  {
   //                   dess=pod.Item.ItemDescription,
   //                   qty=pod.OrderedQuantity,
   //                  };

   //           cboItemDetail.DataSource = aa.ToList();
   //           cboItemDetail.DisplayMember = "dess";
            
         
       //var a = from supplier in ct.Suppliers
       //        join PO in ct.PurchaseOrders
       //        on supplier.SupplierID equals PO.SupplierID


       //        where PO.PurchaseOrderID == c.PurchaseOrderID
       //        select supplier.SupplierName;


  //     label3.Text = a.First().ToString();


       //var d = from itemdes in ct.Items
       //        join pod in ct.PurchaseDetails
       //        on itemdes.ItemID equals pod.ItemID
       //        join PO in ct.PurchaseOrders
       //        on pod.PurchaseOrderID equals PO.PurchaseOrderID

       //        where PO.PurchaseOrderID == c.PurchaseOrderID
       //        select new
       //        {
       //            itemid = itemdes.ItemID,
       //            des = itemdes.ItemDescription,



       //        };
           

       //var aa = d.ToList();

       //cboItemDetail.DataSource = aa;
       //cboItemDetail.DisplayMember ="des";
  
       




    //   cboPOID.DataSource = xx.ToList();
      // cboPOID.DisplayMember = "itemDes";

   
                
            
                //   cboItemDetail.DataSource = xxx;
                // cboItemDetail.DisplayMember = "ItemDescription";

        }

        private void cboItemDetail_SelectedIndexChanged(object sender, EventArgs e)
        {
            Item item = (Item) cboItemDetail.SelectedValue;


            label8.Text = item.ItemID;

            PurchaseDetail pd = purchaseDetails.Find(pDetail => pDetail.ItemID == item.ItemID);
            label9.Text = Convert.ToString(pd.OrderedQuantity);


        // PurchaseDetail pdo = (PurchaseDetail)cboItemDetail.SelectedItem;

        // var xx = from pd in ct.PurchaseDetails

        //          where pd.Item.ItemDescription == pdo.Item.ItemDescription

        //          select new
        //          {
        //              qty = pd.OrderedQuantity,
        //              itemID = pd.ItemID
        //          };

        // foreach (var oo in xx.ToList())
        // {
        //     label8.Text =oo.qty.ToString();
        //     label9.Text = oo.itemID.ToString();
        // }

            //var xx = from pd in ct.PurchaseDetails

            //         join po in ct.PurchaseOrders on pd.PurchaseOrderID equals po.PurchaseOrderID

            //         where po.PurchaseOrderID == pdo.PurchaseOrderID
            //         join it in ct.Items on pd.ItemID equals it.ItemID

            //         select new
            //         {

                       
            //             qty = pd.OrderedQuantity,
            //             itemID = pd.ItemID,
                     

            //         };

            //var list = xx.ToList();

            //foreach (var lll in list)
            //{
            //    label8.Text = lll.qty.ToString();
            //    label9.Text = lll.itemID.ToString();
             
            //}



        }

        private void btn_Update_Click(object sender, EventArgs e)
        {

        }
 

           
      

     
    }
}
